package com.trnnn.renren1.logic;

import javax.servlet.http.HttpServletRequest;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.renren.api.client.RenrenApiClient;
import com.renren.api.client.param.impl.SessionKey;
import com.renren.api.client.services.PhotoService;
import com.renren.api.client.services.UserService;
import com.renren1.session.SessionFactory;

public class UserManager {
	private static final UserManager instance;
	static {
		instance = new UserManager();
	}

	public static UserManager getInstance() {
		return instance;
	}

	public void executeUserInfo() {
		HttpServletRequest request = SessionFactory.REQUEST.get();

		String sessionKey = request.getParameter("xn_sig_session_key");
		String renrenUserId = request.getParameter("xn_sig_user");
		if (sessionKey == null || renrenUserId == null) {
			return;
		}
		RenrenApiClient apiClient = RenrenApiClient.getInstance();
		UserService user = apiClient.getUserService();
		JSONArray jons = user.getInfo(renrenUserId, new SessionKey(sessionKey));
		if (jons != null && jons.size() > 0) {
			JSONObject currentUser = (JSONObject) jons.get(0);
			if (currentUser != null) {
				String userName = (String) currentUser.get("name");
				String userHead = (String) currentUser.get("headurl");
				request.setAttribute("userName", userName);
				request.setAttribute("userHead", userHead);
			}
		}
	}

	public void executePhotos() {
		HttpServletRequest request = SessionFactory.REQUEST.get();

		String sessionKey = request.getParameter("xn_sig_session_key");
		if (sessionKey == null) {
			return;
		}
		// String renrenUserId = request.getParameter("xn_sig_user");
		long uid = Long.parseLong(request.getParameter("xn_sig_user"));
		RenrenApiClient apiClient = RenrenApiClient.getInstance();
		PhotoService photo = apiClient.getPhotoService();
		JSONArray json = photo.getPhotos(uid, 532108904, 1, 40, "",
				new SessionKey(sessionKey));
		request.setAttribute("photos", json.toString());

	}
}
